%% Filter stocks_id in same sector
clc
clear all

% Sector 1: Retail                % Sector 11: Chemistry
% Sector 2: Insurance             % Sector 12: Bank
% Sector 3: Real Estate           % Sector 13: Automobile
% Sector 4: IT                    % Sector 14: Natural Resources
% Sector 5: Oil & Gas             % Sector 15: FMCG
% Sector 6: Financial Services    % Sector 16: Media
% Sector 7: Electricity           % Sector 17: Telecom
% Sector 8: Hospitality           % Sector 18: Construction
% Sector 9: Heavy Industry        % Sector 19: Healthcare
% Sector 10: Household Products               

sector_id = 15;
all_sec_id_ok = get_sec_in_same_sector(sector_id);

sub_sec_id_ok = all_sec_id_ok(1:end);
% Select only 1 stock (327 MSN)
sub_sec_id_ok = 327;

%% Preparation works for TA
mat_X_TA = [];
mat_Y_TA = [];

fromTime = '01/01/2007';
toTime = '31/03/2013';

gap_TA = 7;
gap_ret = 7;   % Tinh return 'gap_ret' ngay (1-4: 5 gap_ret)
window = 37;

for i = 1:size(sub_sec_id_ok,1)
    result = get_info_from_database('security', 'sec_id', ...
        sub_sec_id_ok(i), 'from', fromTime, 'to', toTime);
    if (~isempty(result))
        idx_gap = 1:gap_TA:size(result,1) - window - gap_ret+1;
        
        for j = 1:size(idx_gap,2)
            rslt_gap = result(idx_gap(j):idx_gap(j)+ window + gap_ret-1,:);
            if (~isempty(rslt_gap))
                volume = rslt_gap(:,2);
                hi     = rslt_gap(:,4);
                lo     = rslt_gap(:,5);
                clo    = rslt_gap(:,6);
                
                [pattern] = patternRec3(clo(1:window));
                emaShort  = indicators(clo(1:window),'ema',7);
                emaLong  = indicators(clo(1:window),'ema',34);
                macdArr = indicators(clo(1:window),'macd',12,26,9);
                adxArr = indicators([hi(1:window),lo(1:window),...
                    clo(1:window)],'adx',14);
                cci = indicators([hi(1:window),lo(1:window),...
                    clo(1:window)],'cci',14,20,0.15);
                roc = indicators(clo(1:window),'roc',15);
                rsi = indicators(clo(1:window),'rsi',14);
                willr = indicators([hi(1:window),lo(1:window),...
                    clo(1:window)],'william',14);
                matX = [volume(1:window) clo(1:window) emaShort...
                    emaLong macdArr adxArr cci roc rsi willr];
                
                matX = nanmean(matX,1);
                pattern = pattern(1,:);
                mat_X = [pattern matX];
                
                matY = (clo(end) - clo(window+1))....
                    /clo(window+1);
                
                mat_X_TA = [mat_X_TA ; matX];
                mat_Y_TA = [mat_Y_TA ; matY];
            else
                j;
            end
        end
    end
end
    
%% Delete value 0 in mat_Y_TA
idx_used = find(mat_Y_TA ~= 0);
mat_X_used = mat_X_TA(idx_used,:);
mat_Y_used = mat_Y_TA(idx_used);

%% Stepwise
%stepwise(mat_X_used,mat_Y_used);
%stepwise(mat_X_TA,mat_Y_TA);
%% Tree
t = classregtree(mat_X_TA,mat_Y_TA,'names',{'1','2','3','4','5','6',...
    '7','8','9','10','11','12','13','14'},'minleaf', 50);

view(t)
plot(t.varimportance);
set(gca, 'xticklabel', {'1','2','3','4','5','6',...
    '7','8','9','10'});
